package com.daxia.bumall.dao;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.daxia.bumall.daobase.GenericDAOHibernate;
import com.daxia.bumall.dto.OrderDTO;
import com.daxia.bumall.model.Order;
import com.daxia.bumall.support.Page;

@Repository
public class OrderDAO extends GenericDAOHibernate<Order> {

    public List<Order> find(OrderDTO dto, Page page) {
        StringBuilder hql = new StringBuilder();
        List<Object> paras = new ArrayList<Object>();
        hql.append("From Order n where 1 = 1 ");
        if (dto.getId() != null) {
            hql.append(" and n.id = ? ");
            paras.add(dto.getId());
        }
        if (StringUtils.isNotBlank(dto.getOrderNo())) {
            hql.append(" and n.orderNo = ? ");
            paras.add(dto.getOrderNo());
        }		
        if (dto.getUser() != null) {
        	if (dto.getUser().getId() != null) {
	            hql.append(" and n.user.id = ? ");
	            paras.add(dto.getUser().getId());
        	}
        	if (dto.getUser().getUsername() != null) {
	            hql.append(" and n.user.username like ? ");
	            paras.add("%" + dto.getUser().getUsername() + "%");
        	}
        }			
        if (dto.getOperatorUser() != null) {
        	if (dto.getOperatorUser().getId() != null) {
	            hql.append(" and n.operatorUser.id = ? ");
	            paras.add(dto.getOperatorUser().getId());
        	}
        }			
        if (dto.getFactoryUser() != null) {
        	if (dto.getFactoryUser().getId() != null) {
	            hql.append(" and n.factoryUser.id = ? ");
	            paras.add(dto.getFactoryUser().getId());
        	}
        }			
        if (dto.getPayStatus() != null) {
            hql.append(" and n.payStatus = ? ");
            paras.add(dto.getPayStatus());
        }
        if (dto.getStatus() != null) {
            hql.append(" and n.status = ? ");
            paras.add(dto.getStatus());
        }
        if (dto.getPayType() != null) {
            hql.append(" and n.payType = ? ");
            paras.add(dto.getPayType());
        }
        if (StringUtils.isNotBlank(dto.getReceiveName())) {
            hql.append(" and n.receiveName like ? ");
            paras.add("%" + dto.getReceiveName() + "%");
        }					
        if (StringUtils.isNotBlank(dto.getReceiveMobile())) {
            hql.append(" and n.receiveMobile = ? ");
            paras.add(dto.getReceiveMobile());
        }		
        if (StringUtils.isNotBlank(dto.getExpressCompany())) {
            hql.append(" and n.expressCompany = ? ");
            paras.add(dto.getExpressCompany());
        }		
        if (StringUtils.isNotBlank(dto.getExpressNo())) {
            hql.append(" and n.expressNo = ? ");
            paras.add(dto.getExpressNo());
        }		
        hql.append(" order by n.createTime desc");
        return super.find(hql.toString(), paras.toArray(), page);
    }
    
    public Order findOne(OrderDTO dto) {
		Page page = new Page();
		page.setPageSize(1);
		page.setPage(1);
		
		List<Order> list = this.find(dto, page);
	    return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    public int count(Long user_id){
        return super.queryTotalCount("From Order  where user_id = ? ", new Object[]{user_id});
    }
    
    public int updatePayStatus(Integer status,Long orderId,BigDecimal actualPayAmount) {
    	StringBuffer sqlBuffer = new StringBuffer();
    	Date date = new Date();
    	sqlBuffer.append("update Order set payStatus = ?,actualPayAmount = ?,payTime = ? where id = ? ");
    	Object[] paras = new Object[]{status,actualPayAmount,date,orderId};
    	return super.executeUpdate(sqlBuffer.toString(), paras);
    }
}
